#!/bin/bash
yum install -y rsync git nano htop tmux

#######################################
# Mount the ephemeral SSD storage.
# If four volumes exist, merge them and mount jointly.
# If only two volumes exist, merge them and mount jointly.
# If only one volume exists, just mount that.
# Regardless of how many volumes there are, the name of the folder they are mounted to is the same.
#######################################
# Name of the second volume (if it exists)
SSD2=nvme2n1

# Name of the fourth volume (if it exists)
SSD4=nvme4n1

# Checks if the second volume exists
CHECK2=$(lsblk -l | grep $SSD2)

# Checks if the fourth volume exists
CHECK4=$(lsblk -l | grep $SSD4)

# Checks for four volumes first, then for two volumes
if [[ $CHECK4 ]]
then
  # Fourth SSD volume found
  # Follows https://objectivefs.com/howto/how-to-raid-ec2-instance-stores
  sudo mdadm --create --verbose /dev/md0 --level=0 --name=MY_RAID --chunk=64 --raid-devices=4 /dev/nvme1n1 /dev/nvme2n1 /dev/nvme3n1 /dev/nvme4n1  #requires superuser status to use sudo, and doesn't work without sudo
  sudo mkfs.ext4 -L MY_RAID /dev/md0  #requires sudo to determine file system size
  sudo mkdir -p /mnt/ext  # doesn’t need sudo but adding for consistency
  sudo mount -t ext4 /dev/md0 /mnt/ext  # needs sudo because only root can use --types option

elif [[ $CHECK2 ]]
then
  # Second SSD volume found
  # Follows https://objectivefs.com/howto/how-to-raid-ec2-instance-stores
  sudo mdadm --create --verbose /dev/md0 --level=0 --name=MY_RAID --chunk=64 --raid-devices=2 /dev/nvme1n1 /dev/nvme2n1   #requires superuser status to use sudo, and doesn't work without sudo
  sudo mkfs.ext4 -L MY_RAID /dev/md0  #requires sudo to determine file system size
  sudo mkdir -p /mnt/ext  # doesn’t need sudo but adding for consistency
  sudo mount -t ext4 /dev/md0 /mnt/ext  # needs sudo because only root can use --types option

else
  # Only one SSD volume
  mkfs.ext4 /dev/nvme1n1
  mkdir -p /mnt/ext
  mount -t ext4 /dev/nvme1n1 /mnt/ext
fi


# make temp directory for containers usage
# should be used in the Batch job definition (MountPoints)
mkdir /mnt/ext/tmp
rsync -avPHSX /tmp/ /mnt/ext/tmp/

mkdir -p /var/lib/docker
mkdir -p /mnt/ext/docker

# modify fstab to mount /tmp on the new storage.
sed -i '$ a /mnt/ext/tmp  /tmp  none  bind  0 0' /etc/fstab
sed -i '$ a /mnt/ext/docker /var/lib/docker none  bind  0 0' /etc/fstab
mount -a

# make /tmp usable by everyone
chmod 777 /mnt/ext/tmp

#######################################
# Install docker and docker-compose, per https://acloudxpert.com/how-to-install-docker-compose-on-amazon-linux-ami/
#######################################

yum install -y docker

curl -L https://github.com/docker/compose/releases/download/1.25.4/docker-compose-`uname -s`-`uname -m` | sudo tee /usr/local/bin/docker-compose > /dev/null
chmod +x /usr/local/bin/docker-compose
ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
docker-compose --version

#############################
# Clone latest flux model repo to the home folder
# clone command suggested by Logan Byers. It resolves the problem of not being able to pull the repo after it was cloned, which was conflicting with not being able to SSH into the machine more than ~1 minute after it was created.
# This formulation of git clone makes ec2-user the cloner, rather than root. It's no longer necessary to change ownership (chown) of the repo because carbon-budget will already be owned by ec2-user, not root.
#############################
cd /home/ec2-user
su ec2-user -c "git clone https://github.com/wri/carbon-budget"

#######################################
# Starts the docker service
#######################################
sudo service docker start

# Replaces htop config file with my preferred configuration
mkdir -p /home/ec2-user/.config/htop/
cp /home/ec2-user/carbon-budget/htoprc /home/ec2-user/.config/htop/htoprc